﻿package {

	import flash.display.Sprite;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class MenuItemHandler {
		public static const FOCAL_LENGTH = 350;
		
		private var parentSprite:Sprite;
		private var menuItems:Vector.<MenuItem>;
		
		public function MenuItemHandler(parentSprite:Sprite) {
			this.parentSprite = parentSprite;
			menuItems = new Vector.<MenuItem>;
		}
		
		public function addNewItem(posistion:Vector3D, description:String, url:String) {
			var newMenuItem:MenuItem = new MenuItem();
			
			newMenuItem.xpos3D = posistion.x;
			newMenuItem.ypos3D = posistion.y;
			newMenuItem.zpos3D = posistion.z;
			
			var scaleRatio = FOCAL_LENGTH / (FOCAL_LENGTH + newMenuItem.zpos3D);
			newMenuItem.scaleX = newMenuItem.scaleY = scaleRatio;
			
			// draw the menu items here...
			newMenuItem.x = /*110 +*/ newMenuItem.xpos3D * scaleRatio;
			newMenuItem.y = /*110 +*/ newMenuItem.ypos3D * scaleRatio;
			
			newMenuItem.menuText.text = description;
			newMenuItem.linkTo = url;
			
			newMenuItem.mouseChildren = false;
			newMenuItem.addEventListener(MouseEvent.CLICK, itemClicked);
			
			menuItems.push(newMenuItem);
			
			parentSprite.addChild(newMenuItem);
		}
		
		public function itemClicked(e:Event):void {
			var urlRequest:URLRequest = new URLRequest(e.target.linkTo);
			navigateToURL(urlRequest);
		}
	
	}

}